package com.stu.covid.business.controller;

import com.stu.covid.business.entity.Clazz;
import com.stu.covid.business.entity.Student;
import com.stu.covid.business.service.ClazzService;
import com.stu.covid.business.service.StudentService;
import com.stu.covid.business.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClazzService clazzService;

    @GetMapping("/to-student-list")
    public String toStudentList() {

        return "student_list";
    }

    @GetMapping("/to-student-add")
    public String toStudentAdd(Model model) {
        List<Clazz> clazzList = clazzService.all();
        model.addAttribute("clazzList", clazzList);
        return "student_add";
    }

    @RequestMapping("/student-list")
    @ResponseBody
    public Result<Student> studentList(Student student) {
        return studentService.studentList(student);
    }

    @RequestMapping("/student-add")
    public String studentAdd(Student student) {
        studentService.studentAdd(student);
        return "redirect:to-student-list";
    }

    @RequestMapping("/student-update")
    public String studentUpdate(Student student) {
        studentService.studentUpdate(student);
        return "redirect:to-student-list";
    }

    @RequestMapping("/student-del")
    public String studentDel(Student student) {
        studentService.studentDel(student.getId());
        return "redirect:to-student-list";
    }
}
